﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WorkModel;
using WorkCommon;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Authorization;

namespace WorkApi.Controllers
{

    [Route("api/[controller]")]
    //[Authorize]
    [ApiController]
    public class LoginController : ControllerBase
    {
        private readonly JWTTokenOptions _tokenOptions;
        public LoginController(JWTTokenOptions tokenOptions)
        {
            _tokenOptions = tokenOptions;
        }
       
        [HttpGet]
        [Route("Login")]
        public string Login(string uName, string uPass)
        {
            //模拟数据
            List<Userinfou> list = new List<Userinfou>()
            {
                new Userinfou()  {Uidd=1,Uname="admin",UserPwd="666",UserName="九纹龙",Uqq=null,Uby1=0},
                new Userinfou()  {Uidd=2,Uname="asd123",UserPwd="112233",UserName="李五一",Uqq=null,Uby1=0},
                new Userinfou()  {Uidd=3,Uname="ahusan",UserPwd="152369",UserName="金萱虎",Uqq=null,Uby1=0}
            };
            Userinfou user = new Userinfou();
            //查询
            user = list.Where(p => p.Uname == uName && p.UserPwd == uPass).FirstOrDefault();
            //返回的结果
            bool result = false;
            //返回的token的结果集默认为null
            TokenResult JwtToken = new TokenResult();
            if (user != null)
            {
                //生成teken
                JwtToken = new JwtTokenHelper().AuthorizeToken(uName, _tokenOptions);
                return JsonConvert.SerializeObject(new
                {
                    result = true,
                    JwtToken
                });
            }
            //登录失败，返回默认值 
            return JsonConvert.SerializeObject(new
            {
                result,
                JwtToken
            });
        }
    }
}
